var logger = require("pomelo-logger").getLogger(__filename);

module.exports = function (app) {
    return new Remote(app);
};

var Remote = function (app) {
    this.app = app;
};

Remote.prototype.addNiuniuGameResult = function (msg, callback) {
    logger.info(`Remote addNiuniuGameResult ${JSON.stringify(msg)}`);
    let gameResultService = this.app.get("gameResultService");
    gameResultService.addNiuniuGameResult(msg, gameResult =>{
        if(!!gameResult) {
            logger.info(`Remote addNiuniuGameResult ${JSON.stringify(gameResult)}`);
            callback({resultCode: 0, message: "ok", gameResult: gameResult.toJSON()});
            return;
        }
        callback({resultCode : 404, message: "插入gameRecord失败", gameResult: {}});

    });
};

Remote.prototype.addZhajinhuaGameResult = function (msg, callback) {
    logger.info(`Remote addZhajinhuaGameResult ${JSON.stringify(msg)}`);
    let gameResultService = this.app.get("gameResultService");
    gameResultService.addZhajinhuaGameResult(msg, gameResult =>{
        if(!!gameResult) {
            logger.info(`Remote addZhajinhuaGameResult ${JSON.stringify(gameResult)}`);
            callback({resultCode: 0, message: "ok", gameResult: gameResult.toJSON()});
            return;
        }
        callback({resultCode : 404, message: "插入gameRecord失败", gameResult: {}});

    });
};


Remote.prototype.getNiuniuGameResult = function (msg, callback) {
    logger.info(`Remote getNiuniuGameResult ${JSON.stringify(msg)}`);
    if(!msg.hasOwnProperty("uid")) {
        callback({resultCode: 404, message: "uid为空"});
        return;
    }
    let uid = parseInt(msg.uid);
    let gameResultService = this.app.get("gameResultService");
    gameResultService.getNiuniuGameResult(uid, function (gameResults) {
        callback(gameResultService.serializeGameResults(gameResults));
    });
};

Remote.prototype.getZhajinhuaGameResult = function (msg, callback) {
    logger.info(`Remote getZhajinhuaGameResult ${JSON.stringify(msg)}`);
    if(!msg.hasOwnProperty("uid")) {
        callback({resultCode: 404, message: "uid为空"});
        return;
    }
    let uid = parseInt(msg.uid);
    let gameResultService = this.app.get("gameResultService");
    gameResultService.getZhajinhuaGameResult(uid, function (gameResults) {
        callback(gameResultService.serializeGameResults(gameResults));
    });
};
